package com.funong.matrix.biz.power.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funong.matrix.biz.base.dao.SysRoleDao;
import com.funong.matrix.biz.base.dao.SysRoleUserDao;
import com.funong.matrix.biz.base.entity.SysRoleEntity;
import com.funong.matrix.biz.power.pojo.dto.SysRolePermissionDTO;
import com.funong.matrix.biz.power.pojo.dto.SysRoleUserDTO;
import com.funong.matrix.biz.base.entity.SysRoleUserEntity;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.dto.Result;
import com.funong.matrix.core.enums.IsDeletedEnum;
import com.funong.matrix.core.enums.ResultCodeEnum;
import com.funong.matrix.core.utils.BusinessException;
import com.funong.matrix.core.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleUserDomainDao {
    @Autowired
    private SysRoleUserDao sysRoleUserDao;
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysMenuDomainDao sysMenuDomainDao;
    @Autowired
    private SysRolePermissionDomainDao sysRolePermissionDomainDao;


    public List<SysRoleUserEntity> queryByUserId(Integer userId) {
        return sysRoleUserDao.queryByUserId(userId);
    }

    public void saveUserRole(SysRoleUserDTO sysRoleUserDTO) {
        sysRoleUserDao.deleteRoleUser(sysRoleUserDTO);
        for (String roleCode : sysRoleUserDTO.getRoleCodeList()) {
            SysRoleUserEntity sysRoleUserEntity = new SysRoleUserEntity();
            sysRoleUserEntity
                    .setUserId(sysRoleUserDTO.getUserId())
                    .setRoleCode(roleCode);
            sysRoleUserEntity.setCreatedAt(Timestamp.valueOf(LocalDateTime.now()));
            sysRoleUserEntity.setCreatedBy(JwtUtils.getCurrentUserName());
            sysRoleUserEntity.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
            sysRoleUserEntity.setUpdatedBy(JwtUtils.getCurrentUserName());
            sysRoleUserDao.save(sysRoleUserEntity);
        }
    }

    public List<SysRoleUserEntity> queryByRoleCode(String roleCode) {
        return sysRoleUserDao.queryByRoleCode(roleCode);
    }

    public List<Integer> queryUsersByRoleCode(String roleCode) {
        List<SysRoleUserEntity> roleUserEntities = sysRoleUserDao.queryByRoleCode(roleCode);
        return roleUserEntities.stream().map(SysRoleUserEntity::getUserId).collect(Collectors.toList());
    }

    public SysRoleUserDTO queryRolesByUserId(Integer userId) {
        SysRoleUserDTO sysRoleUserDTO = new SysRoleUserDTO();
        List<SysRoleUserEntity> roleUserEntities = sysRoleUserDao.queryByUserId(userId);
        List<String> roleCodeList = roleUserEntities.stream().map(SysRoleUserEntity::getRoleCode).distinct().collect(Collectors.toList());
        List<String> permissionList = sysRolePermissionDomainDao.queryPermissionListByRoleCodeList(roleCodeList);
        List<String> menuCodeList = sysMenuDomainDao.queryMenuCodeByRoleCode(roleCodeList);
        sysRoleUserDTO
                .setUserId(userId)
                .setRoleCodeList(roleCodeList)
                .setPermissionCodeList(permissionList)
                .setMenuCodeList(menuCodeList)
        ;
        return sysRoleUserDTO;
    }

    public void deleteRoleUser(SysRolePermissionDTO rolePermissionDTO) {
        sysRoleUserDao.deleteRoleUser(rolePermissionDTO);
    }

    public void save(SysRoleUserEntity sysRoleUserEntity) {
        sysRoleUserEntity.setCreatedAt(Timestamp.valueOf(LocalDateTime.now()));
        sysRoleUserEntity.setCreatedBy(JwtUtils.getCurrentUserName());
        sysRoleUserEntity.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        sysRoleUserEntity.setUpdatedBy(JwtUtils.getCurrentUserName());
        sysRoleUserDao.save(sysRoleUserEntity);
    }



    public Boolean saveRole(SysRoleEntity sysRoleEntity) {
        List<SysRoleEntity> roleEntities = sysRoleDao.queryByCode(sysRoleEntity.getCode());
        if (CollectionUtils.isNotEmpty(roleEntities)) {
            throw new BusinessException(ResultCodeEnum.ROLE_CODE_EXISTED);
        }
        sysRoleEntity.setCreatedAt(Timestamp.valueOf(LocalDateTime.now()));
        sysRoleEntity.setCreatedBy(JwtUtils.getCurrentUserName());
        sysRoleEntity.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        sysRoleEntity.setUpdatedBy(JwtUtils.getCurrentUserName());
        return sysRoleDao.save(sysRoleEntity);
    }


    public Boolean deleteRole(SysRoleEntity sysRoleEntity) {
        SysRoleEntity role = sysRoleDao.getById(sysRoleEntity.getId());
        role.setIsDeleted(IsDeletedEnum.DELETED.getValue());
        role.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        role.setUpdatedBy(JwtUtils.getCurrentUserName());
        return sysRoleDao.updateById(role);
    }

    public Boolean updateRole(SysRoleEntity sysRoleEntity) {
        sysRoleEntity.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        sysRoleEntity.setUpdatedBy(JwtUtils.getCurrentUserName());
        return sysRoleDao.updateById(sysRoleEntity);
    }

    public Boolean updateRoleStatus(SysRoleEntity sysRoleEntity) {
        SysRoleEntity role = sysRoleDao.getById(sysRoleEntity.getId());
        role.setStatus(sysRoleEntity.getStatus());
        role.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        role.setUpdatedBy(JwtUtils.getCurrentUserName());
        return sysRoleDao.updateById(role);
    }

    public Result queryRoleList(QueryDTO<SysRoleEntity> roleQO) {
        Page<SysRoleEntity> page = new Page<>(roleQO.getPageNo(), roleQO.getPageSize());
        SysRoleEntity sysRoleEntity = roleQO.getCondition();
        IPage<SysRoleEntity> iPage = sysRoleDao.queryRoleList(page, sysRoleEntity);
        return Result.page(iPage, iPage.getRecords());
    }
}